package com.ruoyi.api.utils;


import com.ruoyi.api.vo.Circle;
import com.ruoyi.api.vo.Coords;
import com.ruoyi.common.core.domain.AjaxResult;
import org.checkerframework.checker.units.qual.C;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 求最小内切圆的半径和圆心
 */
public class SysMath {

    public static Circle mathCircle(List<String> lists){
        List<Coords> listCoords = new ArrayList<>();
        Circle circle = new Circle();
        //将坐标从String类型转换为double类型
        for (String list : lists) {
            Coords coords = new Coords();
            String[] location = list.split(",");
            coords.setLon(Double.valueOf(location[0]));
            coords.setLat(Double.valueOf(location[1]));
            listCoords.add(coords);
        }
        double CenterLat = 0.0;
        double CenterLon = 0.0;
        double radius = 0.0;
        if(listCoords.size() == 1){
            Coords coords1 = listCoords.get(0);
            circle.setCoor(String.valueOf(coords1));
            circle.setRadius("0");
            return circle;
        }else if(listCoords.size() > 1){
            for (int i = 0; i < listCoords.size(); i++) {
                Coords coord = new Coords();
                if(i==0){
                    coord = listCoords.get(i);
                    double a = coord.getLat();
                    double b = coord.getLon();//经度
                    coord = listCoords.get(i+1);
                    double c = coord.getLat();
                    double e = coord.getLon();
                    CenterLat = (a + c)/2;
                    CenterLon = (b + e)/2;
                    radius = GetDistance(b,a,CenterLon,CenterLat);
                }
                if(i>=2){
                    coord = listCoords.get(i);
                    double a = coord.getLat();
                    double b = coord.getLon();//经度
                    double length = GetDistance(b,a,CenterLon,CenterLat);
                    if(length > radius){
                        Coords coords1 = GetCenter(CenterLon,CenterLat,b,a,radius);
                        radius = (radius + length)/2;
                        CenterLat = coords1.getLat();
                        CenterLon = coords1.getLon();
                        circle.setRadius(String.valueOf(radius));
                        circle.setCoor(String.valueOf(String.valueOf(coords1)));
                    }else {
                        circle.setRadius(String.valueOf(radius));
                        Coords coords1 = new Coords();
                        coords1.setLat(CenterLat);
                        coords1.setLon(CenterLon);
                        circle.setCoor(String.valueOf(String.valueOf(coords1)));
                    }
                }
            }
            return circle;
        }
        return null;
    }

    /**
     * @param lon1 第一点的经度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的经度
     * @param lat2 第二点的纬度
     * @return 返回的距离
     * */
    public static double GetDistance(double lon1,double lat1,double lon2, double lat2) {
        double a = Math.abs(lon1 - lon2);
        double b = Math.abs(lat1 - lat2);
        double c = Math.sqrt(a*a + b*b);
        return c;
    }

    /**
     * 求新圆心的位置
     * @param lon1 旧圆心经度
     * @param lat1 旧圆心纬度
     * @param lon2 新添点经度
     * @param lat2 新添点纬度
     * @param length1 旧圆心半径
     * @Param length2 新点到新圆心的半径
     * @return
     */
    public static Coords GetCenter(double lon1,double lat1,double lon2, double lat2,double length1){
        double length = GetDistance(lon2,lat2,lon1,lat1);
        double length2 = (length1 + length)/2;
        Coords coords = new Coords();
        double c = length2/length;
        double a = Math.abs(lon1 - lon2) * c;
        double b = Math.abs(lat1 - lat2) * c;
        if(lon2 > lon1 && lat2 > lat1){
            coords.setLon(lon2 - a);
            coords.setLat(lat2 - b);
        }
        if(lon2 > lon1 && lat2 < lat1){
            coords.setLon(lon2 - a);
            coords.setLat(lat2 + b);
        }
        if(lon2 < lon1 && lat2 > lat1){
            coords.setLon(lon2 + a);
            coords.setLat(lat2 - b);
        }
        if(lon2 < lon1 && lat2 < lat1){
            coords.setLon(lon2 + a);
            coords.setLat(lat2 + b);
        }
        if(lon2 == lon1 && lat2 > lat1){
            coords.setLon(lon2);
            coords.setLat(lat2-b);
        }
        if(lon2 == lon1 && lat2 < lat1){
            coords.setLon(lon2);
            coords.setLat(lat2+b);
        }if(lon2 > lon1 && lat2 == lat1){
            coords.setLon(lon2-a);
            coords.setLat(lat2);
        }if(lon2 < lon1 && lat2 == lat1){
            coords.setLon(lon2 + a);
            coords.setLat(lat2);
        }
        return coords;
    }

}